package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.CourseType;
import com.xmy.cultivate.entity.OrderApply;
import com.xmy.cultivate.entity.StoreGoodsOrder;
import com.xmy.cultivate.entity.StoreGoodsOrderBatch;
import com.xmy.cultivate.service.IStoreGoodsBatchService;
import com.xmy.cultivate.service.IStoreGoodsOrderBatchService;
import com.xmy.cultivate.service.IStoreGoodsOrderService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 加订表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-07-30
 */
@RestController
@RequestMapping("/api/storeGoodsOrder")
public class StoreGoodsOrderController {

    @Autowired
    IStoreGoodsOrderService iStoreGoodsOrderService;

    @Autowired
    IStoreGoodsBatchService iStoreGoodsBatchService;

    @Autowired
    IStoreGoodsOrderBatchService iStoreGoodsOrderBatchService;

    /**
     *已确认加订列表
     * @param pageNum
     * @param pageSize
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param yearClassId
     * @param classTypeId
     * @param subjectsId
     * @param lessonType
     * @param goodsTypeId 类型id，对应类型表
     * @param status
     * @param type 1入库添加，2加订添加
     * @return
     */
    @GetMapping("/getOrderList")
    public Result getOrderList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                               @RequestParam("yearPart") Integer yearPart,
                               @RequestParam("quarterNum") Integer quarterNum,
                               @RequestParam("schoolId") Long schoolId,
                               @RequestParam("yearClassId") Integer yearClassId,
                               @RequestParam("classTypeId") Long classTypeId,
                               @RequestParam("subjectsId") Long subjectsId,
                               @RequestParam("lessonType") Integer lessonType,
                               @RequestParam(value = "goodsTypeId",required = false,defaultValue = "2") Long goodsTypeId,
                               @RequestParam("status") Integer status,
                               @RequestParam(value = "type",required = false,defaultValue = "2") Integer type
    ){

        IPage<OrderApply> iPage = new Page<>(pageNum, pageSize);
        /*iStoreGoodsOrderService.getOrderList(iPage,yearPart,quarterNum,schoolId,yearClassId,classTypeId,subjectsId,lessonType,goodsTypeId,status,type);
        List<StoreGoodsOrder> storeGoodsOrderList = iStoreGoodsOrderService.getOrderListAll(yearPart,quarterNum,schoolId,yearClassId,classTypeId,subjectsId,lessonType,goodsTypeId,status,type,2);
        Integer count = iStoreGoodsOrderService.getMaxLessonNum(yearPart,quarterNum,schoolId,yearClassId,classTypeId,subjectsId,lessonType,goodsTypeId,status,type);
        Map<String ,Object> map = new HashMap<>();
        map.put("list",new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
        map.put("maxLessonNum",count);
        map.put("classTypeList",storeGoodsOrderList);*/

        QueryWrapper<StoreGoodsOrderBatch> storeGoodsOrderBatchQueryWrapper = new QueryWrapper<>();

        storeGoodsOrderBatchQueryWrapper.eq(schoolId!=null,"t1.school_id",schoolId);
        storeGoodsOrderBatchQueryWrapper.eq(yearPart!=null,"t1.year_part",yearPart);
        storeGoodsOrderBatchQueryWrapper.eq(quarterNum!=null,"t1.quarter_num",quarterNum);
        storeGoodsOrderBatchQueryWrapper.eq(yearClassId!=null,"t1.year_class_id",yearClassId);
        storeGoodsOrderBatchQueryWrapper.eq(classTypeId!=null,"t1.class_type_id",classTypeId);
        storeGoodsOrderBatchQueryWrapper.eq(subjectsId!=null,"t1.subjects_id",subjectsId);
        storeGoodsOrderBatchQueryWrapper.eq(lessonType!=null,"t1.lesson_type",lessonType);
        storeGoodsOrderBatchQueryWrapper.eq(goodsTypeId!=null,"t1.goods_type_id",goodsTypeId);
        storeGoodsOrderBatchQueryWrapper.eq(status!=null,"t1.status",status);
        storeGoodsOrderBatchQueryWrapper.eq(type!=null,"t1.type",type);
        storeGoodsOrderBatchQueryWrapper.orderByDesc("t1.id");
        Integer affirmType =1;//1加订确认，2下单，3入库
        iStoreGoodsOrderBatchService.getListForSearch(iPage,storeGoodsOrderBatchQueryWrapper,affirmType);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    /**
     * 获取入库记录
     * @param pageNum
     * @param pageSize
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param yearClassId
     * @param classTypeId
     * @param subjectsId
     * @param lessonType
     * @param goodsTypeId
     * @param status
     * @param type
     * @return
     */
    @GetMapping("/getAddStoreGoodsLog")
    public Result getAddStoreGoodsLog(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                      @RequestParam("yearPart") Integer yearPart,
                                      @RequestParam("quarterNum") Integer quarterNum,
                                      @RequestParam("schoolId") Long schoolId,
                                      @RequestParam("yearClassId") Integer yearClassId,
                                      @RequestParam("classTypeId") Long classTypeId,
                                      @RequestParam("subjectsId") Long subjectsId,
                                      @RequestParam("lessonType") Integer lessonType,
                                      @RequestParam(value = "goodsTypeId",required = false,defaultValue = "2") Long goodsTypeId,
                                      @RequestParam(value = "status",required = false,defaultValue = "3") Integer status,
                                      @RequestParam(value = "type",required = false) Integer type){
        IPage<OrderApply> iPage = new Page<>(pageNum, pageSize);
        /*iStoreGoodsOrderService.getOrderList(iPage,yearPart,quarterNum,schoolId,yearClassId,classTypeId,subjectsId,lessonType,goodsTypeId,status,type);
        List<StoreGoodsOrder> storeGoodsOrderList = iStoreGoodsOrderService.getOrderListAll(yearPart,quarterNum,schoolId,yearClassId,classTypeId,subjectsId,lessonType,goodsTypeId,status,type,2);
        Integer count = iStoreGoodsOrderService.getMaxLessonNum(yearPart,quarterNum,schoolId,yearClassId,classTypeId,subjectsId,lessonType,goodsTypeId,status,type);
        Map<String ,Object> map = new HashMap<>();
        map.put("list",new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
        map.put("maxLessonNum",count);
        map.put("classTypeList",storeGoodsOrderList);*/

        QueryWrapper<StoreGoodsOrderBatch> storeGoodsOrderBatchQueryWrapper = new QueryWrapper<>();

        storeGoodsOrderBatchQueryWrapper.eq(yearPart!=null,"t1.year_part",yearPart);
        storeGoodsOrderBatchQueryWrapper.eq(quarterNum!=null,"t1.quarter_num",quarterNum);
        storeGoodsOrderBatchQueryWrapper.eq(yearClassId!=null,"t1.year_class_id",yearClassId);
        storeGoodsOrderBatchQueryWrapper.eq(classTypeId!=null,"t1.class_type_id",classTypeId);
        storeGoodsOrderBatchQueryWrapper.eq(subjectsId!=null,"t1.subjects_id",subjectsId);
        storeGoodsOrderBatchQueryWrapper.eq(lessonType!=null,"t1.lesson_type",lessonType);
        storeGoodsOrderBatchQueryWrapper.eq(goodsTypeId!=null,"t1.goods_type_id",goodsTypeId);
        storeGoodsOrderBatchQueryWrapper.eq(status!=null,"t1.status",status);
        storeGoodsOrderBatchQueryWrapper.eq(type!=null,"t1.type",type);
        Integer affirmType =3;//1加订确认，2下单，3入库
        iStoreGoodsOrderBatchService.getListForSearch(iPage,storeGoodsOrderBatchQueryWrapper,affirmType);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @GetMapping("/getMaxBatchNum")
    public Result getMaxBatchNum(@RequestParam("yearPart") Integer yearPart,
                                 @RequestParam("quarterNum") Integer quarterNum){
        Integer count = iStoreGoodsBatchService.getMaxBatchNum(yearPart,quarterNum);
        Map<String ,Object> map = new HashMap<>();
        map.put("batchNum",count);
        return new Result(Code.OK, map,"");
    }

    @GetMapping("/getAlreadyOrder")
    public Result getAlreadyOrder(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                  @RequestParam("yearPart") Integer yearPart,
                                  @RequestParam("quarterNum") Integer quarterNum,
                                  @RequestParam("lessonType") Integer lessonType,
                                  @RequestParam("batchNum") Integer batchNum,
                                  @RequestParam("startDate") String startDate,
                                  @RequestParam("endDate") String endDate
                                  ){
        IPage<StoreGoodsOrderBatch> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<StoreGoodsOrderBatch> storeGoodsOrderBatchQueryWrapper = new QueryWrapper<>();
        storeGoodsOrderBatchQueryWrapper.eq(yearPart!=null,"year_part",yearPart);
        storeGoodsOrderBatchQueryWrapper.eq(quarterNum!=null,"quarter_num",quarterNum);
        storeGoodsOrderBatchQueryWrapper.eq(batchNum!=null,"batch_num",batchNum);
        storeGoodsOrderBatchQueryWrapper.eq(lessonType!=null,"lesson_type",lessonType);
        storeGoodsOrderBatchQueryWrapper.ge(StringUtils.isNotBlank(startDate),"affirm_time",startDate+" 00:00:00");
        storeGoodsOrderBatchQueryWrapper.le(StringUtils.isNotBlank(endDate),"affirm_time",endDate+" 23:59:59");
        storeGoodsOrderBatchQueryWrapper.groupBy("batch_num","class_type_id","year_class_id");
        storeGoodsOrderBatchQueryWrapper.orderByDesc("id");

        Integer affirmType =2;//1加订确认，2下单，3入库
        iStoreGoodsOrderBatchService.getAlreadyOrderGroup(iPage,storeGoodsOrderBatchQueryWrapper,affirmType);

        QueryWrapper<StoreGoodsOrder> storeGoodsOrderQueryWrapper = new QueryWrapper<>();
        storeGoodsOrderQueryWrapper.eq(yearPart!=null,"t1.year_part",yearPart);
        storeGoodsOrderQueryWrapper.eq(quarterNum!=null,"t1.quarter_num",quarterNum);
        storeGoodsOrderQueryWrapper.eq(batchNum!=null,"t1.batch_num",batchNum);
        storeGoodsOrderQueryWrapper.eq(lessonType!=null,"t4.lesson_type",lessonType);
        storeGoodsOrderQueryWrapper.ge(StringUtils.isNotBlank(startDate),"t5.created_at",startDate+" 00:00:00");
        storeGoodsOrderQueryWrapper.le(StringUtils.isNotBlank(endDate),"t5.created_at",endDate+" 23:59:59");

        Integer maxLessonNum = iStoreGoodsOrderBatchService.getMaxLessonNum(storeGoodsOrderQueryWrapper,affirmType);

        storeGoodsOrderQueryWrapper.groupBy("t4.class_type_id","t4.year_class_id","t4.lesson_num","t4.lesson_type","t4.subjects_id");
        List<StoreGoodsOrder> storeGoodsOrderList = iStoreGoodsOrderBatchService.getAlreadyOrder(storeGoodsOrderQueryWrapper,affirmType);
        Integer batchNumRs = iStoreGoodsBatchService.getMaxBatchNum(yearPart,quarterNum);
        Map<String ,Object> map = new HashMap<>();
        map.put("list",storeGoodsOrderList);
        map.put("batchNum",batchNumRs);
        map.put("maxLessonNum",maxLessonNum);
        map.put("classTypeList",new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
        return new Result(Code.OK, map,"");

    }

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param batchNum
     * @return
     */
    @GetMapping("/getAlreadyOrderDetail")
    public Result getAlreadyOrderDetail(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                        @RequestParam("batchNum") Integer batchNum){
        IPage<StoreGoodsOrderBatch> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<StoreGoodsOrderBatch> storeGoodsOrderBatchQueryWrapper = new QueryWrapper<>();
        storeGoodsOrderBatchQueryWrapper.inSql("t1.id","select t2.goods_order_batch_id from store_goods_batch as t1 INNER JOIN store_goods_batch_detail as t2 on t1.id = t2.goods_batch_id where t1.batch_num ="+batchNum);

        Integer affirmType =2;//1加订确认，2下单，3入库
        iStoreGoodsOrderBatchService.getListForSearch(iPage,storeGoodsOrderBatchQueryWrapper,affirmType);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

}
